const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const merge = require('webpack-merge');
const webpackBaseConfig = require('./webpack.base.config.js');
// const fs = require('fs');
const path = require('path');

const ENV_JS = path.resolve(__dirname, './src/config/env.js');

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

const HappyPack = require('happypack');

// fs.open(ENV_JS, 'w', (err, fd) => {
// 	const buf = 'export default "production";';
// 	/* eslint-disable-next-line */
// 	fs.write(fd, buf, 0, buf.length, 0, (err, written, buffer) => {});
// 	fs.close(fd);
// });

module.exports = merge(webpackBaseConfig, {
	mode: 'production',
	// devtool: 'source-map', // 开启错误报告 生成完整的 SourceMap，输出为独立文件。
	output: {
		publicPath: '/wx-system/dist/',
		filename: '[name].[chunkhash].js',
		chunkFilename: '[name]-[chunkhash].chunk.js',
	},
	plugins: [
		new HappyPack({
			id: 'babel',
			threads: 4,
			loaders: ['babel-loader?cacheDirectory=false'],
		}),
		// @link https://github.com/webpack-contrib/mini-css-extract-plugin
		new MiniCssExtractPlugin({
			filename: '[name].[hash].css',
			chunkFilename: '[id].[hash].css',
		}),
		// new ExtractTextPlugin({
		// 	filename: '[name].[hash].css',
		// 	allChunks: true,
		// }),
		new HtmlWebpackPlugin({
			filename: '../index.html',
			template: './src/template/index.ejs',
			inject: true,
		}),
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['/js/panx-vconsole.js'],
			publicPath: false,
			append: false,
			hash: true,
		}),
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['https://res.wx.qq.com/open/js/jweixin-1.2.0.js'],
			publicPath: false,
			append: false,
			hash: false,
		}),
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['/static/vendors.wx-system.js'],
			publicPath: false,
			append: false,
			hash: true,
		}),
		new webpack.DefinePlugin({
			'process.env': {
				NODE_ENV: '"production"',
				DEV: process.env.DEV ? 'true' : 'false',
			},
		}),
		/**
         * @link https://github.com/chenchunyong/webpack-dllPlugin 关于dll使用的github地址
         * @link https://doc.webpack-china.org/plugins/dll-plugin/ webpack官方文档
         * @link https://segmentfault.com/a/1190000005969643 例子使用
         */
		new webpack.DllReferencePlugin({
			context: __dirname,
			/* eslint-disable-next-line import/no-dynamic-require */
			manifest: require(path.join(__dirname, '.', 'manifest.json')),
		}),
		new webpack.optimize.ModuleConcatenationPlugin(),

		// @link https://github.com/NMFR/optimize-css-assets-webpack-plugin压缩css并且保留下前缀
		new OptimizeCSSAssetsPlugin({
			assetNameRegExp: /\.css\.*(?!.*map)/g, // 注意不要写成 /\.css$/g
			cssProcessor: require('cssnano'),
			cssProcessorOptions: {
				discardComments: { removeAll: true },
				// 避免 cssnano 重新计算 z-index
				safe: true,
				// cssnano 集成了autoprefixer的功能
				// 会使用到autoprefixer进行无关前缀的清理
				// 关闭autoprefixer功能
				// 使用postcss的autoprefixer功能
				autoprefixer: false,
			},
			canPrint: true,
		}),

		/**
         * watch 编译时忽略这个路径或文件（一定是绝对路径）
         */
		new webpack.WatchIgnorePlugin([
			ENV_JS,
		]),
	],
});
